package xjtu.se.articlecommunity.buser.usermanage;

import org.springframework.web.bind.annotation.*;
import xjtu.se.articlecommunity.buser.usermanage.dto.UserManageDto;
import xjtu.se.articlecommunity.buser.usermanage.dto.UserTab;
import xjtu.se.articlecommunity.common.CommonService;
import xjtu.se.articlecommunity.common.Result;
import xjtu.se.articlecommunity.common.TokenUtils;
import xjtu.se.articlecommunity.common.dto.CurrentUser;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buser/usermanage")
public class UserManageAPI {
    @Resource
    private CommonService commonService;

    @Resource
    private UserManageService service;

    @GetMapping("/queryUserList") // 依据响应条件查询用户列表
    public Result queryUserList(UserManageDto dto){
        Map<String, Object> page = service.queryUserList(dto);
        return Result.success(page);
    }


    @GetMapping("/queryTabs") // 获取用户标签列表
    public Result queryTabList(String u_id){
        if(!service.hasUser(u_id)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "无此用户!");
        }

        List<UserTab> ut_ids = service.queryTabListOfUser(u_id);
        return Result.success(ut_ids);
    }


    @PostMapping("/newTab")  // 新建标签
    public Result newTab(String ut_name){

        return Result.success((Object) service.newTab(ut_name));
    }

    @PostMapping("/delTab") // 删除标签
    public Result delTab(String ut_id){

        if (service.delTab(ut_id)) return Result.success();

        return Result.fail(Result.ERR_CODE_BUSINESS, "删除失败!");
    }

    @PostMapping("/setUserTab")  // 设定用户的标签
    public Result setUserTab(@RequestBody UserManageDto dto){
        if(!service.hasUser(dto.getU_id())){
            return Result.fail(Result.ERR_CODE_BUSINESS, "无此用户!");
        }

        if(!service.hasTabs(dto.getUt_list())){
            return Result.fail(Result.ERR_CODE_BUSINESS, "提供的标签不合法!");
        }

        service.setUserTab(dto.getU_id(), dto.getUt_list());
        return Result.success();
    }


    @PostMapping("/ban") // 封禁用户
    public Result banUser(@RequestParam String u_id){

        if(!service.hasUser(u_id)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "无此用户!");
        }

        if (service.setUserStatus(u_id, -1)){
            return Result.success();
        }

        return Result.fail(Result.ERR_CODE_BUSINESS, "无法封禁用户!");
    }

    @PostMapping("/permit") // 封禁用户
    public Result permitUser(@RequestParam String u_id){

        if(!service.hasUser(u_id)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "无此用户!");
        }

        if (service.setUserStatus(u_id, 1)){
            return Result.success();
        }

        return Result.fail(Result.ERR_CODE_BUSINESS, "无法封禁用户!");
    }

}
